<template>
  <div>
    <!-- 按钮 -->
    <el-row>
      <el-col>
        <el-button type="primary" @click="addDialogVisible = true">添加协议</el-button>
      </el-col>
    </el-row>

    <!-- 添加协议的对话框 -->
    <el-dialog
        title="添加协议"
        :visible.sync="addDialogVisible"
        width="50%"
        @close="addDialogClose">
      <el-form :model="addAgreementForm" :rules="agreementFormRules" ref="addAgreementFormRef" label-width="80px">
        <el-form-item label="协议名称" prop="name">
          <el-input v-model="addAgreementForm.name"></el-input>
        </el-form-item>
        <el-form-item label="协议简述" prop="description">
          <el-input v-model="addAgreementForm.description"></el-input>
        </el-form-item>
        <el-form-item label="附件上传" prop="file">
          <FileInput file-type="pdf" @inputFile="getFile">文件上传</FileInput>
          <el-input v-if="!!addAgreementForm.file" disabled v-model="addAgreementForm.file.name"></el-input>
<!--          <el-input v-model="addAgreementForm.file"></el-input>-->
        </el-form-item>
        <el-form-item label="协议类型" prop="type">
          <el-radio-group v-model="addAgreementForm.type">
            <el-radio  label="0" border>{{agreementType.zero}}</el-radio>
            <el-radio  label="1" border>{{agreementType.one}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAgreement">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
import {uploadAgreementPdf} from "@/network/agreement";
import FileInput from "@/components/common/file/fileInput.vue";
export default {
  name: "AgreementAdd",
  components:{FileInput},
  data() {
    return {
      addDialogVisible:false,
      addAgreementForm: {
        name: '',
        description: '',
        file:'',
        type:''
      },
      agreementType:{
        zero:'用户登录',
        one:'免责声明',
      },
      agreementFormRules: {
        name: [
          {required: true, message: '请输入协议名称', trigger: 'blur'},
          {min: 2, max: 10, message: '长度需要在2到10之间', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请简述协议', trigger: 'blur'}
        ],
        file: [
          {required: true, message: '请上传附件', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请选择协议类型', trigger: 'blur'}
        ],
      },
    }
  },
  methods:{
    getFile(files){
      this.addAgreementForm.file = files[0]
      console.log(this.addAgreementForm.file)
    },
    // 添加协议
    addAgreement() {
      this.$refs.addAgreementFormRef.validate(valid => {
        if (!valid) return;

        uploadAgreementPdf(this.addAgreementForm).then(({code,data,msg}) => {
          if (code !== 200) {
            return this.$errorMessage(msg);
          }
          this.$successMessage('添加协议成功');

          // 隐藏弹窗
          this.addDialogVisible = false;

          // 重新获取协议列表
          this.$emit('update-list');
        })
      })
    },

    // 监听添加角色的弹框的关闭事件
    addDialogClose() {
      this.$refs.addAgreementFormRef.resetFields();
    },
  }
}
</script>

<style scoped>

</style>
